package cn.tedu.orderservice.util;

import cn.tedu.orderservice.pojo.dto.ChargingDto;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class SendMegByMqtt {
    @Autowired
    private MqttClient mqttClient;

    private String topic;

    public void  sendMsg(ChargingDto chargingDto){
        ObjectMapper objectMapper=new ObjectMapper();
        topic="test/start";
        String msg= null;
        try {
            msg = objectMapper.writeValueAsString(chargingDto);
            log.debug("发送-topic:{},内容:{}",topic,msg);
            mqttClient.publish(topic,msg.getBytes(),0,false);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
